package com.water.bs.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.water.bs.service.EmailService;
import com.water.bs.service.OrgService;
import com.water.bs.service.UserService;
import com.water.bs.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

@CrossOrigin
@RestController
public class EmailCodeController {
    @Autowired
    private MailUtil mailUtil;
    @Autowired
    private EmailService emailService;   //之前少写一个@Autowired
    @Autowired
    private UserService userService;
    @Autowired
    private OrgService orgService;

    @RequestMapping("/getEmailCode")  //用户注册验证码
    public JSON getEmailCode (@RequestBody JSONObject userEmail){
        JSONObject rs = new JSONObject();
        Map <String,Object> emailMap = new HashMap<>();
        String email = ((String) userEmail.get("userEmail")).toLowerCase(); //注册的邮箱信息
        int emailRepeat = emailService.isEmailRepeat(email);//判断一下邮箱是否已经注册
        if (emailRepeat>0){
            rs.put("state","2");
            rs.put("message","该邮箱已经被注册!");
            return rs;
        }
        emailService.deleteEmailCode(email.toLowerCase());
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            code+=(random.nextInt(10));
        }
        String sendTime=String.valueOf(System.currentTimeMillis());
        emailMap.put("code",code);
        emailMap.put("sendTime",sendTime);
        emailMap.put("userEmail",email.toLowerCase());
        int i = emailService.saveEmailCode(emailMap);   //把邮箱验证码,发送时间,用户邮箱保存到数据库中
        if (i>0){
            mailUtil.sendMail(code,email);
            rs.put("state","0");
            rs.put("message","邮箱验证码发送成功");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","验证码发送失败");
            return rs;
        }
    }


    @RequestMapping("/getEmailCodeOrg") //机构注册验证码
    public JSON getEmailCodeOrg (@RequestBody JSONObject orgEmail){
        JSONObject rs = new JSONObject();
        Map <String,Object> emailMap = new HashMap<>();
        String email = ((String) orgEmail.get("orgEmail")).toLowerCase(); //注册的邮箱信息
        int emailRepeat = emailService.isEmailRepeatOrg(email);//判断一下邮箱是否已经注册
        if (emailRepeat>0){
            rs.put("state","2");
            rs.put("message","该邮箱已经被注册!");
            return rs;
        }
        emailService.deleteEmailCode(email.toLowerCase());
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            code+=(random.nextInt(10));
        }
        String sendTime=String.valueOf(System.currentTimeMillis());
        emailMap.put("code",code);
        emailMap.put("sendTime",sendTime);
        emailMap.put("userEmail",email.toLowerCase());
        int i = emailService.saveEmailCode(emailMap);   //把邮箱验证码,发送时间,用户邮箱保存到数据库中
        if (i>0){
            mailUtil.sendMail(code,email);
            rs.put("state","0");
            rs.put("message","邮箱验证码发送成功");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","验证码发送失败");
            return rs;
        }
    }
    @RequestMapping("/forgetEmailCodeUser")
    public JSON forgetEmailCodeUser (@RequestParam("email")String email){
        JSONObject rs = new JSONObject();
        System.out.println("忘记密码请求邮件"+email);
        String lowEmail = email.toLowerCase();
        int i1 = userService.isEmailRepeat(lowEmail);
        if (i1<1){
            rs.put("state","2");
            rs.put("message","此邮箱尚未注册!");
            return rs;
        }
        emailService.deleteEmailCode(email.toLowerCase());
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            code+=(random.nextInt(10));
        }
        Map<String,Object> emailMap = new HashMap<>();
        emailMap.put("code",code);
        emailMap.put("sendTime",System.currentTimeMillis());
        emailMap.put("userEmail",email.toLowerCase());
        int i = emailService.saveEmailCode(emailMap);   //把邮箱验证码,发送时间,用户邮箱保存到数据库中
        if (i>0){
            mailUtil.sendMail(code,email);
            rs.put("state","0");
            rs.put("message","邮箱验证码发送成功");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","验证码发送失败");
            return rs;
        }
    }


    @RequestMapping("/forgetEmailCodeOrg")
    public JSON forgetEmailCodeOrg (@RequestParam("email")String email){
        JSONObject rs = new JSONObject();
        System.out.println("忘记密码请求邮件"+email);
        String lowEmail = email.toLowerCase();
        int i1 = orgService.isEmailRepeat(lowEmail);
        if (i1<1){
            rs.put("state","2");
            rs.put("message","此邮箱尚未注册!");
            return rs;
        }
        emailService.deleteEmailCode(email.toLowerCase());
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            code+=(random.nextInt(10));
        }
        Map<String,Object> emailMap = new HashMap<>();
        emailMap.put("code",code);
        emailMap.put("sendTime",System.currentTimeMillis());
        emailMap.put("userEmail",email.toLowerCase());
        int i = emailService.saveEmailCode(emailMap);   //把邮箱验证码,发送时间,用户邮箱保存到数据库中
        if (i>0){
            mailUtil.sendMail(code,email);
            rs.put("state","0");
            rs.put("message","邮箱验证码发送成功");
            return rs;
        }else{
            rs.put("state","1");
            rs.put("message","验证码发送失败");
            return rs;
        }
    }
}

